package com.buba.uploaddemo.controller;

import com.buba.common.result.FileResult;
import com.buba.uploaddemo.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author 张宇飞
 * @Description:
 * @date 2021/9/111:48
 */
@RestController
@RequestMapping("/uploadController")
public class UploadController {
    private UploadService uploadService;


    @RequestMapping("/upload")
    public FileResult upload(MultipartFile file) throws IOException {
        //获取上传的文件名称
        String filename = file.getOriginalFilename();
        //获取时间
        String date = DateTimeFormatter.ofPattern("yyyy/MM/dd").format(LocalDateTime.now());
        //拼接文件名组成新的fileName
        String newFilename = date + System.currentTimeMillis() + filename.substring(filename.lastIndexOf("."));
        //调用业务层的上传方法
        FileResult upload = uploadService.Upload(file.getInputStream(), newFilename);
        return upload;
    }
    @Autowired
    public void setUploadService(UploadService uploadService) {
        this.uploadService = uploadService;
    }
}
